
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()
import { NextResponse } from 'next/server';
import { PageParams } from '@/types';
import { LoveCardType } from '@/types/book';

export async function GET(request: Request) {
  const data = await request.json();
  const { limit = 10, offset = 0 }: PageParams = data;

  const books = await prisma.loveWhoCard.findMany({
    take: limit,
    skip: offset
  })

  return NextResponse.json({ message: '获取成功', data: books }, { status: 200 });
}
// 名字
// name         String

export async function POST(request: Request) {
  const data = await request.json();
  const { id, name, }: LoveCardType = data;

  if (id !== undefined) {
    await prisma.loveWhoCard.update({
      where: { id },
      data: { name, }
    })
  } else {
    // 获取最后一条数据
    const lastData = await prisma.loveWhoCard.findFirst({
      orderBy: { id: 'desc' }
    })
    let newId = 0
    if (lastData) {
      newId = lastData.id + 1
    }
    await prisma.loveWhoCard.create({
      data: { name, id: newId }
    })
  }

  // 返回成功响应
  return NextResponse.json({ message: '提交成功', data: 0 }, { status: 200 });
}

/**
 * 删除
 * @returns 
 */
export async function DELETE(request: Request) {
  const data = await request.json();
  const { id }: LoveCardType = data;

  await prisma.loveWhoCard.delete({
    where: { id }
  })

  // 返回成功响应
  return NextResponse.json({ message: '删除成功', data: 0 }, { status: 200 });
}

/**
 * 批量增加
 */
//

const dataSource = [
   {
      name: "和饭搭子",
   }, {
      name: "和室友",
   }, {
      name: "和发小／闺蜜",
   }, {
      name: "随机挑选1个朋友和TA",
   }, {
      name: "和关系最好的朋友",
   }, {
      name: "和家人",
   }, {
      name: "和好友列表里ID最靠前的朋友",
   }, {
      name: "和离你最近的好友",
   }, {
      name: "和有好感的朋友",
   }, {
      name: "和关系好的异性朋友",
   }, {
      name: "和关系好的同性朋友",
   }, {
      name: "和兄弟姐妹",
   }, {
      name: "和父亲或母亲",
   }, {
      name: "和常联系的朋友",
   }, {
      name: "和爱人",
   }, {
      name: "和现在想到的人",
   }, {
      name: "和好久没联系的同学",
   }, {
      name: "和最近联系的同学",
   }, {
      name: "和认识最久的朋友",
   }, {
      name: "和通讯录里ID最靠前的朋友",
   },
 
]



export async function PUT(request: Request) {
  // 删除所有数据
  await prisma.loveWhoCard.deleteMany()
  // 准备数据
  const data = dataSource.map((item, index) => {
    return {
      name: item.name,
      id: index
    }
  })
  // 批量增加
  await prisma.loveWhoCard.createMany({
    data,
    
  })

  return NextResponse.json({ message: '批量增加成功', data: 0 }, { status: 200 });
}